import '@yunke/jquery_pagination'
import 'angular-ui'
import {
    chain
} from 'lodash';
import angular from 'angular'
import "@yunke/my97datepicker"
import './receivable-fund-ctrl'
import './receivable-invoice-ctrl'
import './receivable-refund-ctrl'
import '../../../public/js/myservice/common/row-toolbar'
import '../../../public/js/myservice/contract/contract-common'
import {
    contractType
} from '../constant'

angular.module('ContractReceivable', ['ngUI', 'ContractReceivableFund', 'rowToolbar', 'ContractCommonService', 'ContractReceivableInvoice', 'ContractReceivableRefund'])
    .controller('contractReceivableCtrl', [
        '$scope', '$filter', 'receivableFundService', 'contractCommonService', '$routeParams', 'receivableInvoiceService', 'receivableRefundService', 'isSpecial',
        'yunkeModal',
        function ($scope, $filter, receivableFundService, contractCommonService, $routeParams, receivableInvoiceService, receivableRefundService, isSpecial, yunkeModal) {
            function modalListItem({ isSplit, remainIncomeAmount, oldFund, title, contractId }) {
                return yunkeModal.open({
                    template: require("./receivable-inc-edit-fund.html"),
                    controller: "receivableModalCtrl", // defined in receivable-fund-ctrl.js
                    title,
                    resolve: {
                        isSplit,
                        remainIncomeAmount,
                        oldFund,
                        contractId
                    }
                })
            }

            const modal = (title, isSplit, remainIncomeAmount) => (contractId, oldFund) => modalListItem({
                oldFund,
                isSplit,
                title,
                contractId,
                remainIncomeAmount
            })
                .promise.then($scope.reloadFunds)

            $scope.modifyItem = modal("修改款项")
            $scope.split = modal("款项拆分", true)
            $scope.add = modal("新增款项", false, $scope.remainIncomeAmount)

            $scope.receivableFundService = receivableFundService;
            $scope.receivableInvoiceService = receivableInvoiceService;
            $scope.receivableRefundService = receivableRefundService;
            $scope.title = 'receivable edit';
            $scope.contractBaseInfo = {};

            const contractId = $routeParams.id;
            //获取合同信息
            contractCommonService.getContract(contractId, function (data) {
                $scope.contractBaseInfo = data.contract;
                $scope.$applyAsync();
            });

            //获取应收信息
            contractCommonService.getContractUnifiedInfo(contractId, function (data) {
                $scope.receivableInfo = data;
                //获取可以添加的剩余收入金额
                $scope.remainIncomeAmount = $filter('remainIncomeAmount2')($scope.receivableInfo.receivables, $scope.contractBaseInfo);
                $scope.$applyAsync();
            });

            //获取可以添加的剩余发票金额
            //$scope.remainInvoiceAmount = $filter('remainInvoiceAmount')($scope.receivableInfo.invoices,$scope.contractBaseInfo);
            //
            $scope.isSpecial = isSpecial

            //款项回写刷新
            $scope.reloadFunds = function () {
                //获取应收信息
                contractCommonService.getContractUnifiedInfo(contractId, function (data) {
                    $scope.receivableInfo.receivables = data.receivables;
                    $scope.$applyAsync();
                });
            };

            //新增发票回写刷新
            $scope.reloadInvoices = function () {
                //获取应收信息
                contractCommonService.getContractUnifiedInfo(contractId, function (data) {
                    $scope.receivableInfo = data;
                    $scope.$applyAsync();
                });
            };

            //新增回款回写刷新
            $scope.reloadRefund = function () {
                //获取应收信息
                contractCommonService.getContractUnifiedInfo(contractId, function (data) {
                    $scope.receivableInfo = data;
                    $scope.$applyAsync();
                });
            };
        }
    ])
    .filter('receivableType', [() => {
        return function (v) {
            return chain(v)
                .split(',')
                .map(i => chain(contractType).find(["id", parseInt(i, 10)]).get('text').value())
                .join(',')
                .value()
        }
    }])
